package com.ynet.middleground.riskmanage.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.riskmanage.bean.InvoiceStatisticQueryReqPager;
import com.ynet.middleground.riskmanage.bean.InvoiceValidateOCRReq;
import com.ynet.middleground.riskmanage.bean.InvoiceValidateReq;
import com.ynet.middleground.riskmanage.dto.InvoiceInfoDTO;
import com.ynet.middleground.riskmanage.dto.InvoiceStatisticDTO;
import com.ynet.middleground.riskmanage.model.InvoiceValidModel;
import com.ynet.middleground.riskmanage.service.InvoiceValidService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description 发票查询接口服务
 * @date 2020-07-31 17:15:28
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "发票查询接口服务", tags = {"发票查询接口服务"})
@Path("invoice")
@Service(timeout = 6000, version = "0.0.1")
public class InvoiceValidlServiceImpl implements InvoiceValidService, GenericService {

    @Autowired
    InvoiceValidModel invoiceValidModel;

    /**
     * 发票基础查验
     *
     * @param req 发票基础查验请求信息
     * @return
     */
    @POST
    @ApiOperation(value = "发票基础查验", notes = "普通查验发票方式")
    @ApiImplicitParam(name = "requestObj", dataType = "InvoiceValidateReq", paramType = "body", required = true)
    @Override
    public ServiceResult<InvoiceInfoDTO> baseValid(@Valid InvoiceValidateReq req) {
        ServiceResult<InvoiceInfoDTO> result = new ServiceResult<>();
        try {
            result.setResult(invoiceValidModel.baseQuery(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "普通查验发票服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * OCR 发票查验
     *
     * @param req OCR 发票查验请求信息
     * @return
     */
    @POST
    @Path("ocr")
    @ApiOperation(value = "OCR 发票查验", notes = "OCR 发票查验，根据发票图片是查验发票信息")
    @ApiImplicitParam(name = "requestObj", dataType = "InvoiceValidateOCRReq", paramType = "body", required = true)
    @Override
    public ServiceResult<InvoiceInfoDTO> ocrValid(@Valid InvoiceValidateOCRReq req) {
        ServiceResult<InvoiceInfoDTO> result = new ServiceResult<>();
        try {
            result.setResult(invoiceValidModel.ocrQuery(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "OCR 查验发票服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 发票统计查询<br>
     * <note> 统计查询服务，分渠道和内管两部分，是为了防止渠道查询所有统计信息 </note>
     *
     * @param req OCR 发票统计查询请求信息
     * @return 统计查询结果列表
     */
    @POST
    @Path("tatistic")
    @ApiOperation(value = "发票统计查询", notes = "发票统计查询")
    @ApiImplicitParam(name = "requestObj", dataType = "InvoiceStatisticQueryReqPager", paramType = "body",
        required = true)
    @Override
    public ServiceResult<List<InvoiceStatisticDTO>> tatisticQuery(@Valid InvoiceStatisticQueryReqPager req) {
        ServiceResult<List<InvoiceStatisticDTO>> result = new ServiceResult<>();
        try {
            result.setResult(invoiceValidModel.tatisticQueryForChannel(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发票统计查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        result.setPagerInfo(req.getPagerInfo());
        return result;
    }

    /**
     * 发票统计查询，内管专用
     *
     * @param req OCR 发票统计查询请求信息
     * @return 统计查询结果列表
     */
    @ApiOperation(value = "发票统计查询，内管专用", notes = "发票统计查询，内管专用", hidden = true)
    @Override
    public ServiceResult<List<InvoiceStatisticDTO>>
        tatisticQueryForPortAdmin(@Valid InvoiceStatisticQueryReqPager req) {
        ServiceResult<List<InvoiceStatisticDTO>> result = new ServiceResult<>();
        try {
            result.setResult(invoiceValidModel.tatisticQueryForPortAdmin(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "发票统计查询服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        result.setPagerInfo(req.getPagerInfo());
        return result;
    }

}